package com.springboot.cloud.demos.producer.rest;

import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.demos.entity.form.DocumentForm;
import com.springboot.cloud.demos.entity.param.DocumentQueryParam;
import com.springboot.cloud.demos.entity.po.Document;
import com.springboot.cloud.demos.producer.service.IDocumentService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/document")
@Api("document")
@Slf4j
public class DocumentController {

    @Autowired
    private IDocumentService documentService;

    @ApiOperation(value = "新增产品", notes = "新增一个产品")
    @ApiImplicitParam(name = "documentForm", value = "新增产品form表单", required = true, dataType = "DocumentForm")
    @PostMapping
    public Result add(@RequestBody DocumentForm documentForm) {
        log.info("name:{}", documentForm);
        Document document = documentForm.toPo(Document.class);
        return Result.success(documentService.add(document));
    }

    @ApiOperation(value = "删除产品", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "产品ID", required = true, dataType = "long")
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable String id) {
        return Result.success(documentService.delete(id));
    }

    @ApiOperation(value = "修改产品", notes = "修改指定产品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "产品ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "documentForm", value = "产品实体", required = true, dataType = "DocumentForm")
    })
    @PutMapping(value = "/{id}")
    public Result update(@PathVariable String id, @Valid @RequestBody DocumentForm documentForm) {
        Document document = documentForm.toPo(Document.class);
        document.setId(id);
        return Result.success(documentService.update(document));
    }

    @ApiOperation(value = "获取产品", notes = "获取指定产品信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "产品ID", required = true, dataType = "long")
    @GetMapping(value = "/{id}")
    public Result<Document> get(@PathVariable String id) {
        log.info("get with id:{}", id);
        return Result.success(documentService.get(id));
    }

    @ApiOperation(value = "查询产品", notes = "根据条件查询产品信息，简单查询")
    @ApiImplicitParam(paramType = "query", name = "unitId", value = "单元Id", required = true, dataType = "string")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @GetMapping
    public Result query(@RequestParam String unitId, @RequestParam Integer page) {
        log.info("query with questionId:{}", unitId);
        DocumentQueryParam param = new DocumentQueryParam(unitId, page);
        List<Document> documentList = documentService.query(param);
        if (!documentList.isEmpty()) return Result.success(documentList.get(0));
        return Result.success();
    }

    @ApiOperation(value = "查询产品", notes = "根据条件查询产品信息，简单查询")
    @ApiImplicitParam(paramType = "query", name = "unitId", value = "单元Id", required = true, dataType = "string")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @GetMapping("/queryByUnit")
    public Result queryByUnit(@RequestParam String unitId) {
        log.info("query with questionId:{}", unitId);
        DocumentQueryParam param = new DocumentQueryParam();
        param.setUnitId(unitId);
        return Result.success(documentService.query(param));
    }
}

